package net.jiupinshi.controller.user;

import com.alibaba.fastjson.JSON;
import net.jiupinshi.common.StaticProperty;
import net.jiupinshi.customAnnotation.LoginAnnotation;
import net.jiupinshi.common.Result;
import net.jiupinshi.dto.UserDTO;
import net.jiupinshi.entity.User;
import net.jiupinshi.service.user.UserService;
import net.jiupinshi.util.SessionUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @description: 请添加描述
 * @author: penghui.guo
 * @create: 2018/9/21 18:50
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @LoginAnnotation
    @RequestMapping("getUserInfo")
    public Result<UserDTO> getUserInfo(HttpServletRequest request) {
        Result<UserDTO> userDTOResult = new Result<>();
        UserDTO user = SessionUtil.getUser(request);
        userDTOResult.setSuccess();
        userDTOResult.setData(user);
        return userDTOResult;
    }

    /**
     * 注册
     * @param name
     * @param password
     * @param remark
     * @param mobile
     * @return
     */
    @RequestMapping("register")
    public Result insertUser(String name, String password, String remark, String mobile){
        Result userDTOResult = new Result();
        User user = new User();
        user.setMobile(mobile);
        user.setName(name);
        user.setRemark(remark);
        user.setPassword(password);
        int i = userService.insertUser(user);
        if(i > 0){
            userDTOResult.setSuccess();
        }
        return userDTOResult;
    }

    /**
     * 更新用户信息
     * @param request
     * @param password
     * @param status
     * @param remark
     * @param mobile
     * @return
     */
    @LoginAnnotation
    @RequestMapping("updateUserInfo")
    public Result updateUserInfo(HttpServletRequest request, String password,
                                 Integer status, String remark, String mobile){
        Result userDTOResult = new Result();
        Long userId = SessionUtil.getUserId(request);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setMobile(mobile);
        user.setRemark(remark);
        user.setPassword(password);
        int i = userService.updateUserInfo(user);
        if(i > 0){
            userDTOResult.setSuccess();
        }
        return userDTOResult;
    }

    /**
     * 登录
     * @param request
     * @param password
     * @param mobile
     * @return
     */
    @RequestMapping("login")
    public Result login(HttpServletRequest request, String password, String mobile){
        Result userDTOResult = new Result();
        UserDTO user = userService.getUserInfoByPwd(mobile, password);
        if(user != null){
            userDTOResult.setSuccess();
            request.getSession().setAttribute(StaticProperty.SESSION_KEY, JSON.toJSONString(user));
        }else{
            userDTOResult.setMsg("登录失败，请重试");
        }
        return userDTOResult;
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public Result logout(HttpServletRequest request){
        Result userDTOResult = new Result();
        userDTOResult.setSuccess();
        request.getSession().removeAttribute(StaticProperty.SESSION_KEY);
        return userDTOResult;
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @LoginAnnotation
    @RequestMapping("getUsers")
    public Result getUsers(){
        Result<List<User>> userDTOResult = new Result<>();
        List<User> users = userService.getUsers();
        userDTOResult.setSuccess();
        userDTOResult.setData(users);
        return userDTOResult;
    }

}
